package com.supermap.shiro.auth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.api.user.LoginService;
import com.supermap.shiro.auth.entity.AppUser;
import com.supermap.shiro.common.AuthFactory;
import com.supermap.shiro.common.ResponseData;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xiaolu
 * @date 2018/6/28 下午4:17
 */
@RestController
@RequestMapping("auth")
public class AuthController {

    @Reference(timeout = 100000)
    private LoginService loginService;

    @RequestMapping(value = "home", method = RequestMethod.GET)
    @ResponseBody
    public String home() {
        return "home";
    }

    @RequestMapping(value = "getLoginUser")
    @ResponseBody
    public ResponseData getLoginUser() {
        JSONObject result = new JSONObject();
        ResponseData responseData = new ResponseData();
        AppUser user = AuthFactory.getLoginUser(true);
        result.put("user", user);
        result.put("token", AuthFactory.getSession().getId());
        responseData.setCode(user == null ? 300 : 200);
        responseData.setData(result);
        responseData.setMsg(user == null ? "未登录" : "登录成功");
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping(value = "userInfo")
    @ResponseBody
    public ResponseData userInfo() {
        JSONObject result = new JSONObject();
        ResponseData responseData = new ResponseData();
        AppUser user = AuthFactory.getLoginUser(false);
        result.put("user", user);
        result.put("token", AuthFactory.getSession().getId());
        responseData.setCode(user == null ? 300 : 200);
        responseData.setData(result);
        responseData.setMsg(user == null ? "未登录" : "登录成功");
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping(value = "error", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData log() {
        ResponseData responseData = new ResponseData();
        responseData.setCode(300);
        responseData.setData(null);
        responseData.setMsg("用户未登录！");
        responseData.setSuccess(true);
        return responseData;
    }

    @RequestMapping("/login")
    public ResponseData login(String userName, String password, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        ResponseData responseData = new ResponseData();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userName, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            //完成登录
            subject.login(usernamePasswordToken);
            AppUser user = ((AppUser) subject.getPrincipal()).clone();
            user.setRealPassword(null);
            user.setPassword(null);
            result.put("user", user);
            result.put("token", subject.getSession().getId());
            responseData.setCode(200);
            responseData.setData(result);
            responseData.setMsg("登录成功！");
            responseData.setSuccess(true);
        } catch (Exception e) {
            //返回登录页面
          Integer reulet=  loginService.getUserLoginNameIsNotExist(userName);
          if(reulet>0) {
              responseData.setMsg("密码错误！");
          }else{
              responseData.setMsg("账号不存在！");
          }
            responseData.setCode(100);
            responseData.setData(e.getMessage());
            responseData.setSuccess(false);
            e.printStackTrace();
        }
        return responseData;
    }

    @RequestMapping("/logout")
    public ResponseData logout() {
        ResponseData responseData = new ResponseData();
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        responseData.setCode(200);
        responseData.setData(null);
        responseData.setMsg("注销成功！");
        responseData.setSuccess(true);
        return responseData;
    }
}
